--
-- $Id$
--

--
-- polis ids
--
CREATE SEQUENCE polis_ids;

GRANT ALL ON polis_ids TO polisuser;

--
-- polis data
--
CREATE TABLE polis_data (
	id		NUMERIC,
	name		VARCHAR(64),
	description	TEXT,
	summary		TEXT,

	updated		TIMESTAMP,
	created		TIMESTAMP,
	deleted		TIMESTAMP
);

--
-- polis next id
--
CREATE FUNCTION polis_next_id ()
  RETURNS INTEGER
  AS '                                \
    BEGIN                             \
      RETURN NEXTVAL(''polis_ids'');  \
    END;                              \
  ' LANGUAGE plpgsql;

--
-- polis created
--
CREATE FUNCTION polis_created ()
  RETURNS TRIGGER
  AS '                      \
    BEGIN                   \
      NEW.created = NOW();  \
      RETURN NEW;           \
    END;                    \
  ' LANGUAGE plpgsql;

--
-- polis updated
--
CREATE FUNCTION polis_updated ()
  RETURNS TRIGGER
  AS '                      \
   BEGIN                    \
      NEW.updated = NOW();  \
      RETURN NEW;           \
    END;                    \
  ' LANGUAGE plpgsql;

--
-- polis deleted
--
CREATE FUNCTION polis_deleted ()
  RETURNS TRIGGER
  AS '                          \
    BEGIN                       \
--      NEW.deleted = NOW(); -- \
--      RETURN NEW;          -- \
      RETURN OLD;               \
    END;                        \
  ' LANGUAGE plpgsql;
